<?hh
<<__EntryPoint>>
function main_entry(): void {
  require_once("dom_test.inc");

  $dom = new DOMDocument;
  $dom->loadXML(getXmlStr());
  if(!$dom) {
    echo "Error while parsing the document\n";
    exit;
  }

  $node = $dom->documentElement;
  echo "Tag Name: ".$node->tagName."\n";


  $node->setAttribute('num', '1');
  echo "Chapter: ".$node->getAttribute('num')."\n";
  echo 'Attribute num exists?: '.($node->hasAttribute('num')?'Yes':'No')."\n";
  $node->removeAttribute('num');
  echo "Chapter: ".$node->getAttribute('num')."\n";
  echo 'Attribute num exists?: '.($node->hasAttribute('num')?'Yes':'No')."\n";

  echo "Language: ".$node->getAttribute('language')."\n";
  $lang = $node->getAttributeNode('language');
  $lang->nodeValue = 'en-US';
  $node->setAttributeNode($lang);
  echo "Language: ".$node->getAttribute('language')."\n";
  $node->removeAttributeNode($lang);
  echo "Language: ".$node->getAttribute('language')."\n";

  echo "\n-- xml:lang --\n";
  $node->setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:lang', 'en');
  echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
  echo 'Attribute xml:lang exists?: '.($node->hasAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')?'Yes':'No')."\n";

  $node->removeAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang');
  echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
  echo 'Attribute xml:lang exists?: '.($node->hasAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')?'Yes':'No')."\n";

  $lang = $dom->createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:lang');
  $lang->nodeValue = 'en-GB';
  $node->setAttributeNodeNS($lang);
  unset($lang);
  echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
  $lang = $node->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang');
  echo "Language: ".$lang->value."\n";

  echo "\n-- Elements --\n";
  $rows = $node->getElementsByTagName('row');
  echo "Row Count: ".$rows->length."\n";

  $element_ns = new DOMElement('newns:myelement', 'default content', 'urn::dummyns');
  $node->appendChild($element_ns);
  $element_ns = new DOMElement('newns2:myelement', 'second default content', 'urn::dummyns');
  $node->appendChild($element_ns);

  $myelements = $node->getElementsByTagNameNS('urn::dummyns', 'myelement');
  $mylen = $myelements->length;
  echo "myelements Count: ".$mylen."\n";

  echo "\n-- IDs --\n";
  $node->setAttribute('idatt', 'n1');
  $node->setIDAttribute('idatt', TRUE);

  for ($x = 0; $x < $mylen; $x++) {
  	$current = $myelements->item($x);
  	(string)($current->setAttributeNS('urn::dummyns', 'newns:idatt', 'n'.($x+2)))."\n";
  	$current->setIDAttributeNS('urn::dummyns', 'idatt', TRUE);
  }

  echo 'Element Name: '.(($elem = $dom->getElementById('n1'))?$elem->localName:'Not Found')."\n";
  $idatt = $node->getAttributeNode('idatt');
  $node->setIDAttributeNode($idatt, FALSE);
  echo 'Element Name: '.(($elem = $dom->getElementById('n1'))?$elem->localName:'Not Found')."\n";

  echo 'Element Name: '.(($elem = $dom->getElementById('n3'))?$elem->nodeName:'Not Found')."\n";
  for ($x = 0; $x < $mylen; $x++) {
  	$node = $myelements->item($x);
  	$node->setIDAttributeNS('urn::dummyns', 'idatt', FALSE);
  }
  echo 'Element Name: '.(($elem = $dom->getElementById('n3'))?$elem->nodeName:'Not Found')."\n";
}
